/**
 * 
 */
package br.com.gedbrazil.gedweb.modell.dao;

import java.sql.Connection;
import java.sql.SQLException;

import br.com.gedbrazil.gedweb.framework.dao.CreateDaoException;
import br.com.gedbrazil.gedweb.framework.dao.DaoHelper;
import br.com.gedbrazil.gedweb.modell.entity.User;

/**
 * @author andre
 *
 */
public class UserDao {
	
	private DaoHelper daoHelper;
		
	public UserDao(){
		daoHelper = new DaoHelper();
	}
	
	public void insert (User user) throws CreateDaoException{
		
		Connection conn = null;
		
		try {
			
			daoHelper.beginTransaction();
			conn = daoHelper.getConnectionFromContext();
	
			String query ="insert into user(nome, usuario, senha, nivel_acesso)values(?, ?, ?, ?)";
			long id = daoHelper.executePreparedUpdateReturnGeneratedKey(conn, query
																			  , user.getNome()
																			  , user.getUsuario()
																			  , user.getSenha()
																			  , user.getNivelAcesso());
			user.setId(id);
			
			daoHelper.endTransaction();
			
		} catch (SQLException e) {
			
			daoHelper.rollbackTransaction();
			throw new CreateDaoException("Não foi possivel realizar a transação", e);
		} 
		
	}

}
